#include "StdAfx.h"
#include "MenuItemsHolder.h"
#include <gdiplus.h>

using namespace Gdiplus;

#pragma warning(disable:4482)

MenuItemsHolder::MenuItemsHolder()
{
	GdiplusStartup(&m_GdiPlusToken, &m_GdiPlusStartupInput, NULL);

	HINSTANCE hInstance = _AtlBaseModule.GetResourceInstance();

	for (int i = 0; i < MAX_MENU_ICONS; i++)
	{
		m_Icons[i] = NULL;
	}

	m_Icons[MenuPics::ICON_NONE] = NULL;
	m_Icons[MenuPics::ICON_FROG] = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_FROG), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	m_Icons[MenuPics::ICON_EXTRACT_FROM] = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_EXTRACT_FROM_FOLDER), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	m_Icons[MenuPics::ICON_EXTRACT_TO] = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_EXTRACT_TO_FOLDER), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	m_Icons[MenuPics::ICON_COPY] = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_COPY), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	m_Icons[MenuPics::ICON_DELETE] = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_DELETE), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	m_Icons[MenuPics::ICON_TOUCH] = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_TOUCH), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	m_Icons[MenuPics::ICON_ENCRYPT] = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_ENCRYPT), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	m_Icons[MenuPics::ICON_DECRYPT] = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_DECRYPT), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	m_Icons[MenuPics::ICON_JOIN] = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_JOIN), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	m_Icons[MenuPics::ICON_SPLIT] = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_SPLIT), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	m_Icons[MenuPics::ICON_SECURITY] = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_SECURITY), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	m_Icons[MenuPics::ICON_PICTURES] = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_PICTURES), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	m_Icons[MenuPics::ICON_IMAGE_TOPIC] = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_IMAGE_TOPIC), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	m_Icons[MenuPics::ICON_IMAGE_SAVE] = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_IMAGE_SAVE), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	m_Icons[MenuPics::ICON_IMAGE_EDIT] = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_IMAGE_EDIT), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	m_Icons[MenuPics::ICON_LIST_FULL] = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_LIST_DETAILED), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	m_Icons[MenuPics::ICON_LIST_SIMPLE] = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_LIST_SIMPLE), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	m_Icons[MenuPics::ICON_RENAME] = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_RENAME), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	m_Icons[MenuPics::ICON_HEART] = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_HEART), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	m_Icons[MenuPics::ICON_HELP] = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_HELP), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	m_Icons[MenuPics::ICON_BOOK] = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_BOOK), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	m_Icons[MenuPics::ICON_SHRINK] = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_SHRINK), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	m_Icons[MenuPics::ICON_MOVE] = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_MOVE), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	m_Icons[MenuPics::ICON_ORGANIZE] = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_ORGANIZE), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	m_Icons[MenuPics::ICON_PDF] = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_PDF), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	m_Icons[MenuPics::ICON_REN_ABC] = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_RENAME_ABC), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	m_Icons[MenuPics::ICON_REN_NUM] = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_RENAME_NUM), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	m_Icons[MenuPics::ICON_REN_REPLACE] = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_RENAME_REPLACE), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	m_Icons[MenuPics::ICON_REN_TRUNC] = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_RENAME), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	m_Icons[MenuPics::ICON_SPLIT_JOIN] = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_SPLITJOIN), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	m_Icons[MenuPics::ICON_FAV] = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_FAV), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	m_Icons[MenuPics::ICON_SETTINGS] = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_SETTINGS), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	m_Icons[MenuPics::ICON_SEPARATOR] = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_SEPARATOR), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	m_Icons[MenuPics::ICON_ABOUT] = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_ABOUT), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	m_Icons[MenuPics::ICON_INVERT] = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_INVERT), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	m_Icons[MenuPics::ICON_COPY_PATH] = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_COPY_PATH), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	m_Icons[MenuPics::ICON_SORT_DESKTOP] = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_SORTDESKTOP), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	m_Icons[MenuPics::ICON_SORT_DESKTOP_REDO] = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_SORTDESKTOP_REDO), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	m_Icons[MenuPics::ICON_THEME_DARK] = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_THEME_DARK), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	m_Icons[MenuPics::ICON_THEME_EGGPLANT] = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_THEME_EGGPLANT), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	m_Icons[MenuPics::ICON_THEME_SKY] = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_THEME_SKY), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	m_Icons[MenuPics::ICON_THEME_SUNSET] = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_THEME_SUNSET), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
	m_Icons[MenuPics::ICON_MOVE_UP] = (HICON)LoadImage(hInstance, MAKEINTRESOURCE(IDI_ICON_MOVE_UP), IMAGE_ICON, 16, 16, LR_DEFAULTCOLOR);
		
	for (int i = 0; i < MAX_MENU_ICONS; i++)
	{
		m_Bitmaps[i] = IconToBitmapPARGB32(m_Icons[i]);
	}
	
	m_CmdText[FileOps::ExtractFilesToFolder] = L"Extract Files to Folder";
	m_CmdText[FileOps::ExtractFilesFromFolder] = L"Extract Files from Folder";
	m_CmdText[FileOps::CopyFiles] = L"Copy";
	m_CmdText[FileOps::MoveFiles] = L"Move";
	m_CmdText[FileOps::DeleteZeroLengthFiles] = L"Delete Empty Files\\Folders";
	m_CmdText[FileOps::Touch] = L"Touch";

	m_CmdText[FileOps::FilesListDetailed] = L"Detailed";
	m_CmdText[FileOps::FilesListDetailedFullPath] = L"Detailed - Include Full Path";
	m_CmdText[FileOps::FilesListSimple] = L"Simple";
	m_CmdText[FileOps::FilesListSimpleFullPath] = L"Simple - Include Full Path";

	m_CmdText[FileOps::EncryptFiles] = L"Encrypt";
	m_CmdText[FileOps::DecryptFiles] = L"Decrypt";
	m_CmdText[FileOps::SplitFiles] = L"Split";
	m_CmdText[FileOps::JoinFiles] = L"Join";

	m_CmdText[FileOps::RenameFindReplace] = L"Rename - Find and Replace";
	m_CmdText[FileOps::RenameAutoNumbering] = L"Rename - Autonumbering";
	m_CmdText[FileOps::RenameAlphabetize] = L"Rename - Alphabetize";
	m_CmdText[FileOps::RenameAppend] = L"Rename - Append";

	m_CmdText[FileOps::RenameAutoNumberingWithAppend] = L"Rename - Autonumbering with Append";
	m_CmdText[FileOps::RenameAlphabetizeWithAppend] = L"Rename - Alphabetize with Append";

	m_CmdText[FileOps::RenameMakeSameName] = L"Rename - Make All Same Name";

	m_CmdText[FileOps::RenameTruncate] = L"Rename - Truncate";	

	m_CmdText[FileOps::ShrinkImageFiles] = L"Image - Custom Resize";

	m_CmdText[FileOps::ShrinkImageFiles25] = L"Image - Resize 25%";
	m_CmdText[FileOps::ShrinkImageFiles50] = L"Image - Resize 50%";
	m_CmdText[FileOps::ShrinkImageFiles75] = L"Image - Resize 75%";
	m_CmdText[FileOps::ShrinkImageFiles25Jpg] = L"Image - Resize 25% (Save as Jpeg)";
	m_CmdText[FileOps::ShrinkImageFiles50Jpg] = L"Image - Resize 50% (Save as Jpeg)";
	m_CmdText[FileOps::ShrinkImageFiles75Jpg] = L"Image - Resize 75% (Save as Jpeg)";
	m_CmdText[FileOps::ShrinkImageFiles100Jpg] = L"Image - Convert to Jpeg";	

	m_CmdText[FileOps::PdfImageAlbum] = L"Image Album - Custom";	
	m_CmdText[FileOps::PdfImageAlbumPhotoWallet] = L"Image Album - Fill Page, Wallet";	
	m_CmdText[FileOps::PdfImageAlbumPhoto3_5x5] = L"Image Album - Fill Page, 3.5x5";	
	m_CmdText[FileOps::PdfImageAlbumPhoto4x6] = L"Image Album - Fill Page, 4x6";	
	m_CmdText[FileOps::PdfImageAlbumPhoto5x7] = L"Image Album - Fill Page, 5x7";	
	m_CmdText[FileOps::PdfImageAlbumPhoto8x10] = L"Image Album - Fill Page, 8x10";	
	m_CmdText[FileOps::PdfImageAlbumPhoto9x12] = L"Image Album - Fill Page, 9x12";	
	m_CmdText[FileOps::PdfImageAlbumTwice8x10] = L"Image Album - Page 320x464, Photo 8x10";	
	m_CmdText[FileOps::PdfImageAlbumQuad4x6] = L"Image Album - Page 320x464, Photo 4x6";	

	m_CmdText[FileOps::InvertSelection] = L"Invert Selection";	
	m_CmdText[FileOps::CopyFilePath] = L"Copy Path to Clipboard";

	m_CmdText[FileOps::SortDesktop] = L"Sort Desktop - Custom";
	m_CmdText[FileOps::SortDesktopRedo] = L"Sort Desktop - Redo Last Sort";
	m_CmdText[FileOps::SortDesktopEggPlant] = L"Sort Desktop - EggPlant";	
	m_CmdText[FileOps::SortDesktopSunset] = L"Sort Desktop - Sunset";	
	m_CmdText[FileOps::SortDesktopDark] = L"Sort Desktop - Dark";	
	m_CmdText[FileOps::SortDesktopBlueSky] = L"Sort Desktop - Blue Sky";		

	m_CmdText[FileOps::AddLogoToImage] = L"Put Logo on Image";			

	m_CmdText[FileOps::MoveUp] = L"Move Files Up";		

	m_CmdText[FileOps::MenuSeparator] = L"Menu Separator";

	m_CmdText[FileOps::Contents] = L"Contents";
	m_CmdText[FileOps::About] = L"About";
	m_CmdText[FileOps::Settings] = L"Settings";

	m_CmdText[FileOps::ManageFavorites] = L"Manage Favorites";

	m_CmdIcon[FileOps::ExtractFilesToFolder] = MenuPics::ICON_EXTRACT_TO;
	m_CmdIcon[FileOps::ExtractFilesFromFolder] = MenuPics::ICON_EXTRACT_FROM;
	m_CmdIcon[FileOps::CopyFiles] = MenuPics::ICON_COPY;
	m_CmdIcon[FileOps::MoveFiles] = MenuPics::ICON_MOVE;
	m_CmdIcon[FileOps::DeleteZeroLengthFiles] = MenuPics::ICON_DELETE;
	m_CmdIcon[FileOps::Touch] = MenuPics::ICON_TOUCH;

	m_CmdIcon[FileOps::FilesListDetailed] = MenuPics::ICON_LIST_FULL;
	m_CmdIcon[FileOps::FilesListDetailedFullPath] = MenuPics::ICON_LIST_FULL;
	m_CmdIcon[FileOps::FilesListSimple] = MenuPics::ICON_LIST_SIMPLE;
	m_CmdIcon[FileOps::FilesListSimpleFullPath] = MenuPics::ICON_LIST_SIMPLE;

	m_CmdIcon[FileOps::EncryptFiles] = MenuPics::ICON_ENCRYPT;
	m_CmdIcon[FileOps::DecryptFiles] = MenuPics::ICON_DECRYPT;
	m_CmdIcon[FileOps::SplitFiles] = MenuPics::ICON_SPLIT;
	m_CmdIcon[FileOps::JoinFiles] = MenuPics::ICON_JOIN;

	m_CmdIcon[FileOps::RenameFindReplace] = MenuPics::ICON_REN_REPLACE;
	m_CmdIcon[FileOps::RenameAutoNumbering] = MenuPics::ICON_REN_NUM;
	m_CmdIcon[FileOps::RenameAlphabetize] = MenuPics::ICON_REN_ABC;
	m_CmdIcon[FileOps::RenameAppend] = MenuPics::ICON_RENAME;

	m_CmdIcon[FileOps::RenameAutoNumberingWithAppend] = MenuPics::ICON_REN_NUM;
	m_CmdIcon[FileOps::RenameAlphabetizeWithAppend] = MenuPics::ICON_REN_ABC;

	m_CmdIcon[FileOps::RenameMakeSameName] = MenuPics::ICON_RENAME;

	m_CmdIcon[FileOps::RenameTruncate] = MenuPics::ICON_REN_TRUNC;

	m_CmdIcon[FileOps::ShrinkImageFiles] = MenuPics::ICON_SHRINK;

	m_CmdIcon[FileOps::ShrinkImageFiles25] = MenuPics::ICON_SHRINK;
	m_CmdIcon[FileOps::ShrinkImageFiles50] = MenuPics::ICON_SHRINK;
	m_CmdIcon[FileOps::ShrinkImageFiles75] = MenuPics::ICON_SHRINK;
	m_CmdIcon[FileOps::ShrinkImageFiles25Jpg] = MenuPics::ICON_SHRINK;
	m_CmdIcon[FileOps::ShrinkImageFiles50Jpg] = MenuPics::ICON_SHRINK;
	m_CmdIcon[FileOps::ShrinkImageFiles75Jpg] = MenuPics::ICON_SHRINK;
	m_CmdIcon[FileOps::ShrinkImageFiles100Jpg] = MenuPics::ICON_IMAGE_SAVE;

	m_CmdIcon[FileOps::PdfImageAlbum] = MenuPics::ICON_PDF;
	
	m_CmdIcon[FileOps::PdfImageAlbumPhotoWallet] = MenuPics::ICON_PDF;
	m_CmdIcon[FileOps::PdfImageAlbumPhoto3_5x5] = MenuPics::ICON_PDF;
	m_CmdIcon[FileOps::PdfImageAlbumPhoto4x6] = MenuPics::ICON_PDF;
	m_CmdIcon[FileOps::PdfImageAlbumPhoto5x7] = MenuPics::ICON_PDF;
	m_CmdIcon[FileOps::PdfImageAlbumPhoto8x10] = MenuPics::ICON_PDF;
	m_CmdIcon[FileOps::PdfImageAlbumPhoto9x12] = MenuPics::ICON_PDF;
	m_CmdIcon[FileOps::PdfImageAlbumTwice8x10] = MenuPics::ICON_PDF;
	m_CmdIcon[FileOps::PdfImageAlbumQuad4x6] = MenuPics::ICON_PDF;

	m_CmdIcon[FileOps::InvertSelection] = MenuPics::ICON_INVERT;
	m_CmdIcon[FileOps::CopyFilePath] = MenuPics::ICON_COPY_PATH;
	
	m_CmdIcon[FileOps::MenuSeparator] = MenuPics::ICON_SEPARATOR;

	m_CmdIcon[FileOps::Contents] = MenuPics::ICON_BOOK;
	m_CmdIcon[FileOps::About] = MenuPics::ICON_ABOUT;
	m_CmdIcon[FileOps::Settings] = MenuPics::ICON_SETTINGS;

	m_CmdIcon[FileOps::ManageFavorites] = MenuPics::ICON_FAV;

	m_CmdIcon[FileOps::SortDesktop] = MenuPics::ICON_SORT_DESKTOP;
	m_CmdIcon[FileOps::SortDesktopRedo] = MenuPics::ICON_SORT_DESKTOP_REDO;

	m_CmdIcon[FileOps::SortDesktopEggPlant] = MenuPics::ICON_THEME_EGGPLANT;
	m_CmdIcon[FileOps::SortDesktopSunset] = MenuPics::ICON_THEME_SUNSET;
	m_CmdIcon[FileOps::SortDesktopDark] = MenuPics::ICON_THEME_DARK;
	m_CmdIcon[FileOps::SortDesktopBlueSky] = MenuPics::ICON_THEME_SKY;

	m_CmdIcon[FileOps::AddLogoToImage] = MenuPics::ICON_IMAGE_EDIT;	

	m_CmdIcon[FileOps::MoveUp] = MenuPics::ICON_MOVE_UP;
}

MenuItemsHolder::~MenuItemsHolder()
{
	for (int i = 0; i < MAX_MENU_ICONS; i++) 
	{
		if (m_Icons[i] != NULL)
		{
			DestroyIcon(m_Icons[i]);
		}

		if (m_Bitmaps[i] != NULL)
		{
			DeleteObject(m_Bitmaps[i]);
		}
	}

	if (m_GdiPlusToken)
	{
		GdiplusShutdown(m_GdiPlusToken);
	}
}

struct Pixel
{
	BYTE b, g, r, a;
};

bool MenuItemsHolder::HasAlpha(HBITMAP hbm)
{
	DIBSECTION ds;
	
	if (GetObject(hbm, sizeof(ds), &ds) != sizeof(ds))
	{
		return false;
	}

	if (ds.dsBmih.biBitCount != 32)
	{
		return false;
	}

	LONG pixels = ds.dsBmih.biWidth * abs(ds.dsBmih.biHeight);
	Pixel* begin = static_cast<Pixel*>(ds.dsBm.bmBits);
	Pixel* end = begin + pixels;
	for (Pixel* p =begin; p !=end; ++p)
	{
		if (p->a !=0)
		{
			return true;
		}
	}

	return false;
}

HBITMAP MenuItemsHolder::ConvertDDBToDIB(HBITMAP hbm)
{
	// Is it a valid bitmap?
	BITMAP bm;

	if (GetObject(hbm, sizeof(bm), &bm) !=sizeof(bm))
	{
		return 0;
	}

	// Is it already a DIB section?
	if (bm.bmBits)
	{
		return hbm;
	}

	// Create the new DIB.
	HDC hdc = GetDC(0);
	BITMAPINFO bi = {sizeof(BITMAPINFOHEADER)};
	bi.bmiHeader.biWidth = bm.bmWidth;
	bi.bmiHeader.biHeight = -bm.bmHeight; // top-down
	bi.bmiHeader.biPlanes = 1;
	bi.bmiHeader.biBitCount = bm.bmBitsPixel;
	bi.bmiHeader.biCompression = BI_RGB;
	
	if (bi.bmiHeader.biBitCount < 24)
	{
		bi.bmiHeader.biBitCount = 24;
	}

	void * bits =0;
	HBITMAP dib = CreateDIBSection(hdc, &bi, DIB_RGB_COLORS, &bits, 0, 0);
	
	if (dib)
	{
		GetDIBits(hdc, hbm, 0, abs(bi.bmiHeader.biHeight), bits, &bi, DIB_RGB_COLORS);
	}

	ReleaseDC(0, hdc);
	return dib;
};

HBITMAP MenuItemsHolder::CreatePARGBFromHICON(HICON hIcon, UINT size)
{
	using namespace Gdiplus;

	// Create GDI+ bitmap from icon, preserving alpha channel.
	ICONINFO ii;
	GetIconInfo(hIcon, &ii);
	HBITMAP dib = ConvertDDBToDIB(ii.hbmColor);
	DIBSECTION ds;
	GetObject(dib, sizeof(ds), &ds);
	Bitmap* sourceBitmap;
	
	if (HasAlpha(dib))
	{
		sourceBitmap = new Bitmap(ds.dsBm.bmWidth, ds.dsBm.bmWidth,	ds.dsBm.bmWidthBytes, PixelFormat32bppARGB, static_cast<BYTE*>(ds.dsBm.bmBits));
	}
	else
	{
		sourceBitmap = new Bitmap(hIcon);
	}

	DeleteObject(ii.hbmColor);
	DeleteObject(ii.hbmMask);

	// Rescale bitmap to target size.
	Bitmap scaledBitmap(size, size, PixelFormat32bppARGB);
	Graphics graphics(&scaledBitmap);
	graphics.SetInterpolationMode(InterpolationModeHighQualityBilinear);
	graphics.DrawImage(sourceBitmap, 0, 0, scaledBitmap.GetWidth(), scaledBitmap.GetHeight());

	// Extract and return the GDI bitmap.
	HBITMAP hbm =0;
	scaledBitmap.GetHBITMAP(Color(0, 0, 0), &hbm);
	delete sourceBitmap;
	DeleteObject(dib);
	return hbm;
}

HBITMAP MenuItemsHolder::IconToBitmapPARGB32(HICON hIcon)
{
	if (hIcon)
	{		
		return CreatePARGBFromHICON(hIcon, 16);
	}
	else
	{
		return NULL;
	}
}
